<?php

namespace App\Api\V1\Controllers;


use App\Models\Comment;
use App\Models\News;
use App\User;
use Auth;
use Carbon\Carbon;
use Illuminate\Http\Request;


class NewsController extends BaseController
{
    /**
     * 新闻详情
     * @param Request $request
     * @return mixed
     */
    public function get_news_info(Request $request)
    {
        $id = $request->input("id");
        $page = $request->input("page");
        $news = News::find($id);
        $user = User::isLogin();
        $news->view_num = $news->view_num + 1;
        $voters_count = $news->countUpVoters();
        $news->zan_num = $voters_count;
        $news->save();
        $news->content = $news->detail->content;
        unset($news->detail);
        $news->time = Carbon::parse($news->created_at)->diffForHumans();
        $media = $news->media()->first(['id', 'name', 'avatar']);
        $news->media = $media;
        $news->voters = $news->voters()->limit(30)->get(['id', 'name', 'avatar']);
        $news->comments_count = $news->comments()->count();
        if (!$user) {
            $news->is_follow = false;
            $news->is_vote = false;
        } else {
            $news->is_follow = $user->isFollowing($media);
            $news->is_vote = $user->hasVoted($news);
        }


        $news->comments = $news->comments()->paginate(10, ['id', 'user_id', 'parent_id', 'body', 'created_at']);
        foreach ($news->comments as $k => $v) {
            $news->comments[$k]['user'] = $v->user()->first(['id', 'name', 'avatar']);
            $news->comments[$k]->time = Carbon::parse($v->created_at)->diffForHumans();
        }

        if ($page > 1) {
            unset($news->content);
        }


        return $this->response->array(['status_code' => 200, 'message' => '请求成功', 'data' => $news]);
    }

    public function add_comment(Request $request)
    {
        $news_id = $request->input("news_id");
        $body = $request->input("body");
        $user_id = Auth::id();
        $parent_id = $request->input("comment_id", 0);
        $news = News::findOrFail($news_id);
        $comment = new Comment([
            'body' => $body,
            'user_id' => $user_id,
            'parent_id' => $parent_id
        ]);

        $re = $news->comments()->save($comment);

        return $this->response->array(['status_code' => 200, 'message' => '评论成功', 'data' => $re]);
    }

    /**
     * 赞新闻
     * @param Request $request
     * @return mixed
     */
    public function zan(Request $request)
    {
        $id = $request->input("id");

        $news = News::find($id);
        $user = Auth::user();
        if ($user->hasVoted($news)) {

            return $this->response->array(['status_code' => 0, 'message' => '已经赞过了', 'data' => $id]);
        } else {
            if ($user->upVote($news)) {
                return $this->response->array(['status_code' => 200, 'message' => '点赞成功', 'data' => $id]);
            } else {
                return $this->response->array(['status_code' => 0, 'message' => '点赞失败', 'data' => $id]);
            }

        }


    }

}